var btoa;


function bjca_mssp_charArray2Str_sm2(arr) {
  var r = "";
  for (var i = 0; i < arr.length; i++) {
    var d = arr[i] & 0xff;
    if (d < 16) {
      r = r + "0";
    }
    r += d.toString(16);

  }
  return r;

}

function bjca_mssp_charArray2Str_sm4(arr) {
  var r = "";
  for (var i = 0; i < arr.length; i++) {
    var d = parseInt(arr[i], 16) & 0xff;

    if (d < 16) {
      r = r + "0";
    }
    r += d.toString(16);

  }
  return r;

}

function bjca_mssp_str2charArray(arr) {
  var r = new Array();
  for (var i = 0; i < arr.length; i++) {
    r.push(arr.charCodeAt(i));

  }
  return r;

}

function bjca_mssp_hex2charArray(arr) {
  var r = new Array();
  for (var i = 0; i < arr.length; i += 2) {
    r.push(parseInt(arr.substr(i, 2), 16));

  }
  return r;

}

function bjca_mssp_uint32ToStr(result) {
  var ret = new Array();

  for (var i = 0; i < result.length; i++) {
    var ri = result[i];
    ret.push(((ri >>> 24) & 0xff));
    ret.push(((ri >>> 16) & 0xff));
    ret.push(((ri >>> 8) & 0xff));
    ret.push((ri & 0xff));
  }

  return bjca_mssp_charArray2Str_sm2(ret);

}

var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
var BI_RC = new Array();
var rr, vv;
rr = "0".charCodeAt(0);
for (vv = 0; vv <= 9; ++vv)
  BI_RC[rr++] = vv;
rr = "a".charCodeAt(0);
for (vv = 10; vv < 36; ++vv)
  BI_RC[rr++] = vv;
rr = "A".charCodeAt(0);
for (vv = 10; vv < 36; ++vv)
  BI_RC[rr++] = vv;

function int2char(n) {
  return BI_RM.charAt(n);
}
function intAt(s, i) {
  var c = BI_RC[s.charCodeAt(i)];
  return (c == null) ? -1 : c;
}

function hex2str(hex) {
  var r = "";
  for (var i = 0; i < hex.length; i += 2) {
    var d = parseInt(hex.substr(i, 2), 16) & 0xff;
    r = r + String.fromCharCode(d);
  }
  return r;

}

　　function stringToHex(str) {
  var val = "";
  var r;
  for (var i = 0; i < str.length; i++) {
    r = str.charCodeAt(i);
    if (r < 0x10)
      val = val + "0" + r.toString(16);
    else
      val = val + r.toString(16);

  }
  return val;

}

function HexArray2Str(hex) {
  var r = "";
  for (var i = 0; i < hex.length; i++) {
    var d = hex[i] & 0xff;
    if (d < 16) {
      r = r + "0";
    }
    r = r + d.toString(16);
  }
  return r;

}

function hexStrToUint8Array(hexStr) {
  if (!hexStr || hexStr.length % 2 != 0) {
    return null
  }
  var a = [];
  for (var i = 0; i < hexStr.length; i += 2) {
    a.push(parseInt("0x" + hexStr.substr(i, 2), 16))
  }
  return new Uint8Array(a)
}

function base64EncodeUint8Array(uint8Array) {
  var CHUNK_SIZE = 32768;
  var index = 0;
  var length = uint8Array.length;
  var result = "";
  var slice;
  var slice_tmp = "";
  while (index < length) {
    slice = uint8Array.subarray(index, Math.min(index + CHUNK_SIZE, length));
    for (var i = 0,
      len = slice.length; i < len; i++) {
      slice_tmp += String.fromCharCode(slice[i])
    }
    result += slice_tmp;
    index += CHUNK_SIZE;
    slice_tmp = ""
  }
  return btoa(result)
}
(function () {
  // base64 character set, plus padding character (=)
  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /=",

    // Regular expression to check formal correctness of base64 encoded strings
    b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;

  btoa =function (string) {
    string = String(string);
    var bitmap, a, b, c,
      result = "",
      i = 0,
      rest = string.length % 3; // To determine the final padding

    for (; i < string.length;) {
      if ((a = string.charCodeAt(i++)) > 255 ||
        (b = string.charCodeAt(i++)) > 255 ||
        (c = string.charCodeAt(i++)) > 255)
        throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");

      bitmap = (a << 16) | (b << 8) | c;
      result += b64.charAt(bitmap >> 18 & 63) + b64.charAt(bitmap >> 12 & 63) +
        b64.charAt(bitmap >> 6 & 63) + b64.charAt(bitmap & 63);
    }

    // If there's need of padding, replace the last 'A's with equal signs
    return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
  };
})()

module.exports = {
  hexStrToUint8Array,
  base64EncodeUint8Array,
  bjca_mssp_charArray2Str_sm4,
}